package com.egg.ylt.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.fragment.FRA_CollectNews;
import com.egg.ylt.fragment.FRA_CollectShop;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ACT_CollectCentre extends BaseActivity {

    @BindView(R.id.iv_collect_back)
    ImageView ivCollectBack;
    @BindView(R.id.tv_news)
    TextView tvNews;
    @BindView(R.id.line_collect_news)
    View lineHealth;
    @BindView(R.id.rl_news)
    RelativeLayout rlNews;
    @BindView(R.id.tv_shop)
    TextView tvShop;
    @BindView(R.id.line_collect_shop)
    View lineDiary;
    @BindView(R.id.rl_shop)
    RelativeLayout rlShop;
    @BindView(R.id.ll_top_view)
    LinearLayout llTopView;
    @BindView(R.id.fragment_collect)
    FrameLayout fragmentCollect;

    private FRA_CollectNews mNews;
    private FRA_CollectShop mShop;

    //fragment对象
    private Fragment currentFragment;

    //标记
    private ImageView ivCurrent;
    private TextView tvCurrent;


    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_collect_centre;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, llTopView);


        //初始化fragment
        mNews = new FRA_CollectNews();
        mShop = new FRA_CollectShop();

        //设置初始显示的页面导航图标
        tvShop.setSelected(true);
        tvCurrent = tvShop;

        // 设置主页显示的页面
        if (!mNews.isAdded()) {
            currentFragment = mShop;
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_collect, mShop);
            transaction.commitAllowingStateLoss();
        }

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }


    @OnClick({R.id.iv_collect_back, R.id.rl_news, R.id.rl_shop})
    public void onViewClicked(View view) {

        tvCurrent.setSelected(false);

        switch (view.getId()) {
            case R.id.iv_collect_back:// 返回键
                finish();
                break;
            case R.id.rl_news://咨询
                tvNews.setSelected(true);
                tvCurrent = tvNews;
                switchContent(currentFragment, mNews);
                lineHealth.setBackgroundColor(mContext.getResources().getColor(R.color.color_FE752D));
                lineDiary.setBackgroundColor(mContext.getResources().getColor(R.color.color_FFFFFF));
                break;
            case R.id.rl_shop://店铺
                tvShop.setSelected(true);
                tvCurrent = tvShop;
                switchContent(currentFragment, mShop);
                lineHealth.setBackgroundColor(mContext.getResources().getColor(R.color.color_FFFFFF));
                lineDiary.setBackgroundColor(mContext.getResources().getColor(R.color.color_FE752D));
                break;
        }
    }


    /**
     * 切换Fragment
     *
     * @param from
     * @param to
     */
    public void switchContent(Fragment from, Fragment to) {
        if (currentFragment != to) {
            currentFragment = to;
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from).add(R.id.fragment_collect, to).commitAllowingStateLoss(); // 隐藏当前的fragment，add
            } else {
                transaction.hide(from).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
            }
        }
    }
}
